package cn.edu.hqu.example.bj.aispeakl.light;


import android.os.AsyncTask;

import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Timer;
import java.util.TimerTask;

/**
 * CSDN：http://blog.csdn.net/Jsagacity
 * 这个就是客户端发送数据包
 */


public class dolight extends AsyncTask<String, Void, Void> {

    //这里是连接ESP8266的IP和端口号，IP是通过指令在单片机开发板查询到，而端口号可以自行设置，也可以使用默认的，333就是默认的
    private static final String IP = "192.168.100.52";
    private static final int PORT = 8899;


    private Socket client = null;
    private PrintStream out = null;

    @Override
    protected Void doInBackground(String... params) {
        String str = params[0];
        try {
            client = new Socket(IP, PORT);
            client.setSoTimeout(5000);

            // 获取Socket的输出流，用来发送数据到服务端
            out = new PrintStream(client.getOutputStream());
            out.print(str);
            out.flush();

            Timer timer=new Timer();//实例化Timer类
            timer.schedule(new TimerTask(){
                public void run(){
                    System.out.println("退出");
                    this.cancel();}},200);


            if (client == null) {
                return null;
            } else {
                out.close();
                client.close();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
